package com.asj.house.controller;

import com.asj.house.client.HouseIntermediaryClient;
import com.asj.house.domain.HouseIntermediary;
import com.asj.house.domain.HouseRoom;
import com.asj.house.returns.RespCode;
import com.asj.house.returns.RespEntity;
import com.asj.house.returns.RespTable;
import com.asj.house.utils.OSSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * <p>
 * 中介信息配置 前端控制器
 * </p>
 *
 * @author asj
 * @since 2021-03-25
 */
@RestController
@RequestMapping("/house-intermediary")
@Slf4j
@Api(value = "中介信息配置相关接口", tags = "中介信息配置相关接口")
public class HouseIntermediaryController {

    @Resource
    private HouseIntermediaryClient houseIntermediaryClient;

    @Resource
    private OSSUtil ossUtil;

    @ApiOperation(value = "中介信息列表")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "page", value = "当前页数", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "limit", value = "每页数量", required = true)
    })
    public RespTable listHouseIntermediary(@RequestParam(value = "page", required = true) Integer page,
                                           @RequestParam(value = "limit", required = true) Integer limit) {
        RespTable respTable = houseIntermediaryClient.listHouseIntermediary(page, limit);
        return respTable;
    }

    @GetMapping("/search")
    @ApiOperation(value = "搜索中介信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "page", value = "当前页数", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "limit", value = "每页数量", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "name", value = "中介姓名", required = true)
    })
    public RespTable searchHouseIntermediary(@RequestParam(value = "page", required = true) Integer page,
                                             @RequestParam(value = "limit", required = true) Integer limit,
                                             @RequestParam(value = "name", required = true) String name) {
        RespTable respTable = houseIntermediaryClient.searchHouseIntermediary(page, limit, name);
        return respTable;
    }

    /**
     * OSS文件上传
     * */
    @RequestMapping("/upload")
    @ApiOperation(value = "上传中介头像")
    public RespEntity upload(MultipartFile file){
        String fileName = ossUtil.getFileName("house/head/", file.getOriginalFilename());
        String url = ossUtil.getUrl(fileName);
        try {
            byte[] bytes = file.getBytes();
            ossUtil.upload(fileName, bytes);
            return new RespEntity(RespCode.SUCCESS, url);
        } catch (Exception e) {
            log.error("文件上传失败", e.getMessage());
            return new RespEntity(RespCode.UPLOAD_ERROR);
        }
    }

    @PostMapping("/insert")
    @ApiOperation(value = "新增中介信息")
    public RespEntity insertHouseIntermediary(HouseIntermediary houseIntermediary) {
        houseIntermediaryClient.insertIntermediary(houseIntermediary);
        return new RespEntity(RespCode.SUCCESS);
    }

    @PutMapping("/update")
    @ApiOperation("编辑中介信息")
    public RespEntity updateHouseIntermediary(HouseIntermediary houseIntermediary) {
        houseIntermediaryClient.updateIntermediary(houseIntermediary);
        return new RespEntity(RespCode.SUCCESS);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除中介信息")
    public RespEntity deleteHouseIntermediary(@RequestParam("id") Integer id) {
        houseIntermediaryClient.deleteIntermediary(id);
        return new RespEntity(RespCode.SUCCESS);
    }

    @DeleteMapping("/deleteBatch")
    @ApiOperation(value = "批量删除中介信息")
    public RespEntity deleteBatchHouseIntermediary(@RequestParam("ids") Integer[] ids) {
        houseIntermediaryClient.deleteBatchIntermediary(ids);
        return new RespEntity(RespCode.SUCCESS);
    }
}
